package manager;

import helper.Credential;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import util.HibernateUtil;

public class LoginManager {

	Session session = null;
	
	public Credential authenticateUser(String username, String password){
		session = HibernateUtil.prepareSession();
		String QUERY = "select new helper.Credential(firstName, lastName, id, 'patient', username) " +
				"from PatientGeneralInfo where username='"+username+"' and password = '"+password+"'";
		
		Query hql = session.createQuery(QUERY);
		List<Credential> lstCredential = hql.list();
		
		QUERY = "select new helper.Credential(firstName, lastName, id, 'doctor', username) " +
			"from Doctor where username='"+username+"' and password = '"+password+"'";
		
		hql = session.createQuery(QUERY);
		lstCredential.addAll(hql.list());
		
		if(lstCredential.size() == 0){
			return null;
		}else{
			return lstCredential.get(0);
		}
	}
	
	
	public static void main(String[] args){
		System.out.println(new LoginManager().authenticateUser("aravind", "aravind"));
	}
	
}
